/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mintandleaf.webkits.core;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Wrap all servle't object and can be retrieved via thread local
 *
 * @author teguhh
 **/
public final class WebKitsModuleContext {
    private static ThreadLocal<WebKitsModuleContext> contextThread=
            new ThreadLocal<WebKitsModuleContext>();


    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;
    private ServletContext context;

    /**
     * Get ServletContext object
     * @return ServletContext object
     **/
    public ServletContext getContext() {
        return context;
    }

    /**
     * Set ServletContext object
     * @param context ServletContex object
     **/
    public void setContext(ServletContext context) {
        this.context = context;
    }

    /**
     * Get HttpServletRequest object
     * @return HttpServletRequest object
     **/
    public HttpServletRequest getRequest() {
        return request;
    }

    /**
     * Set HttpRequest object
     * @param request HttpServletRequest object
     **/
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * Get HttpServletResponse object
     * @return ServletContext object
     **/
    public HttpServletResponse getResponse() {
        return response;
    }

    /**
     * Set HttpServletResponse object
     * @param response HttpServletResponse
     **/
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * Get HttpSession object
     * @return HttpSession object
     **/
    public HttpSession getSession() {
        return session;
    }

    /**
     * Set HttpSession object
     * @param session HttpSession object
     **/
    public void setSession(HttpSession session) {
        this.session = session;
    }

    /**
     * Get current ActionContext
     *
     **/
    public static WebKitsModuleContext getWebKitsActionContext(){
        return contextThread.get();
    }

    /**
     * Initiate all servlet object as thread local
     * @param paramRequest HttpServletRequest object
     * @param paramResponse HttpServletResponse object
     * @param paramSession HttpSession object
     * @param paramContext ServletContext object
     *
     **/
    static void setWebKitsActionContext(HttpServletRequest paramRequest,
            HttpServletResponse paramResponse,HttpSession paramSession,
            ServletContext paramContext){
            WebKitsModuleContext webKitsActionContext=
                    new WebKitsModuleContext();
            webKitsActionContext.setRequest(paramRequest);
            webKitsActionContext.setResponse(paramResponse);
            webKitsActionContext.setSession(paramSession);
            webKitsActionContext.setContext(paramContext);
            contextThread.set(webKitsActionContext);
    }

    /**
     * Remove all servlet object from thread local
     **/
    static void remove(){
        contextThread.remove();
    }
}
